﻿using DevComponents.DotNetBar.SuperGrid;
using Kimd.Common;
using Kimd.Logic;
using Kimd.Motion;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;
using WorkStation.Logic;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace WorkStation.UI.Controls
{
    public partial class Tester : UserControl
    {
        public int TesterIndex { get; set; }
        public Tester()
        {
            InitializeComponent();
        }

        private void Tester_Resize(object sender, EventArgs e)
        {
            if(this.Width > 0)
            {
                this.sC_Leftright.SplitterDistance = this.sC_Leftright.Width / 2;
            }
            if(this.Height > 0)
            {
                this.sC_Updown.SplitterDistance = this.sC_Updown.Height  * 3/ 4;
            }
        }

        public void AddTesterIn()
        {
            this.sGC_InputStatus.PrimaryGrid.Columns["Status"].EditorType = typeof(MyGridImageEditControl);
            this.sGC_InputStatus.PrimaryGrid.Columns["Status"].EditorParams = new object[] { iL_Input, ImageSizeMode.CenterImage };
            this.superGridControl1.PrimaryGrid.Columns["Status"].EditorType = typeof(MyGridImageEditControl);
            this.superGridControl1.PrimaryGrid.Columns["Status"].EditorParams = new object[] { iL_Input, ImageSizeMode.CenterImage };

            foreach (TesterStatusInFlag value in Enum.GetValues(typeof(TesterStatusInFlag)))
            {
                GridRow gridRow2 = new GridRow();

                GridCell gridCell1 = new GridCell();
                gridCell1.Value = EnumHelper.GetEnumDescriptionFromValue(value);
                gridRow2.Cells.Add(gridCell1);

                GridCell gridCell2 = new GridCell();
                gridCell2.Value = "圆形未选中32.png";
                gridCell2.ReadOnly = true;

                gridRow2.Cells.Add(gridCell2);

                this.sGC_InputStatus.PrimaryGrid.Rows.Add(gridRow2);
            }

            foreach (TesterActionInFlag value in Enum.GetValues(typeof(TesterActionInFlag)))
            {
                GridRow gridRow2 = new GridRow();

                GridCell gridCell1 = new GridCell();
                gridCell1.Value = EnumHelper.GetEnumDescriptionFromValue(value);
                gridRow2.Cells.Add(gridCell1);

                GridCell gridCell2 = new GridCell();
                gridCell2.Value = "圆形未选中32.png";
                gridCell2.ReadOnly = true;

                gridRow2.Cells.Add(gridCell2);

                this.superGridControl1.PrimaryGrid.Rows.Add(gridRow2);
            }
        }

        public void AddHandlerOut()
        {
            foreach (HandlerControlOutFlag value in Enum.GetValues(typeof(HandlerControlOutFlag)))
            {
                GridRow gridRow2 = new GridRow();
                gridRow2.Tag = value;

                GridCell gridCell2 = new GridCell();
                gridCell2.Value = EnumHelper.GetEnumDescriptionFromValue(value);
                gridRow2.Cells.Add(gridCell2);

                GridCell gridCell3 = new GridCell();
                gridCell3.Value = false;
                gridRow2.Cells.Add(gridCell3);

                this.sGC_OutputStatus.PrimaryGrid.Rows.Add(gridRow2);
            }
            foreach (HandlerActionOutFlag value in Enum.GetValues(typeof(HandlerActionOutFlag)))
            {
                GridRow gridRow2 = new GridRow();
                gridRow2.Tag = value;

                GridCell gridCell2 = new GridCell();
                gridCell2.Value = EnumHelper.GetEnumDescriptionFromValue(value);
                gridRow2.Cells.Add(gridCell2);

                GridCell gridCell3 = new GridCell();
                gridCell3.Value = false;
                gridRow2.Cells.Add(gridCell3);

                this.superGridControl2.PrimaryGrid.Rows.Add(gridRow2);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            TesterStatusLogic testerLogic = (FlowManager.Instance.SelectFlow($"测试机状态{TesterIndex + 1}") as TesterStatusLogic);
            foreach (var row in sGC_InputStatus.PrimaryGrid.Rows)
            {
                (row as GridRow).Cells[1].Value = testerLogic.IsInStatus(EnumHelper.GetEnumValueFromDescription<TesterStatusInFlag>((row as GridRow).Cells[0].Value.ToString())) ? "圆形选中32.png" : "圆形未选中32.png";
            }
            foreach (var row in superGridControl1.PrimaryGrid.Rows)
            {
                (row as GridRow).Cells[1].Value = testerLogic.IsInAction(EnumHelper.GetEnumValueFromDescription<TesterActionInFlag>((row as GridRow).Cells[0].Value.ToString())) ? "圆形选中32.png" : "圆形未选中32.png";
            }
            l_Cell1Scan.Text = EnumHelper.GetEnumDescriptionFromValue(testerLogic.GetCellScanStatus(0));
            l_Cell2Scan.Text = EnumHelper.GetEnumDescriptionFromValue(testerLogic.GetCellScanStatus(1));
            l_Cell3Scan.Text = EnumHelper.GetEnumDescriptionFromValue(testerLogic.GetCellScanStatus(2));
            l_Cell4Scan.Text = EnumHelper.GetEnumDescriptionFromValue(testerLogic.GetCellScanStatus(3));

            l_Cell1Result.Text = EnumHelper.GetEnumDescriptionFromValue(testerLogic.GetCellResultStatus(0));
            l_Cell2Result.Text = EnumHelper.GetEnumDescriptionFromValue(testerLogic.GetCellResultStatus(1));
            l_Cell3Result.Text = EnumHelper.GetEnumDescriptionFromValue(testerLogic.GetCellResultStatus(2));
            l_Cell4Result.Text = EnumHelper.GetEnumDescriptionFromValue(testerLogic.GetCellResultStatus(3));
        }

        private void sGC_OutputStatus_CellValueChanged(object sender, GridCellValueChangedEventArgs e)
        {
            HandlerControlOutFlag control = (HandlerControlOutFlag)e.GridCell.GridRow.Tag;

            if (e.GridCell.GridColumn.Name.Equals("Trigger"))
            {
                if (Convert.ToBoolean(e.GridCell.Value))
                {
                    int data = 0;
                    data = GlobalVariable.HandlerControlData[TesterIndex];
                    data |= 0x01 << (int)control;
                    GlobalVariable.HandlerControlData[TesterIndex] = (short)data;
                }
                else
                {
                    int data = 0;
                    data = GlobalVariable.HandlerControlData[TesterIndex];
                    data &= ~(0x01 << (int)control);
                    GlobalVariable.HandlerControlData[TesterIndex] = (short)data;
                }
            }
        }

        private void superGridControl2_CellValueChanged(object sender, GridCellValueChangedEventArgs e)
        {
            HandlerActionOutFlag control = (HandlerActionOutFlag)e.GridCell.GridRow.Tag;

            if (e.GridCell.GridColumn.Name.Equals("Trigger"))
            {
                if (Convert.ToBoolean(e.GridCell.Value))
                {
                    int data = 0;
                    data = GlobalVariable.HandlerActionData[TesterIndex];
                    data |= 0x01 << (int)control;
                    GlobalVariable.HandlerActionData[TesterIndex] = (short)data;
                }
                else
                {
                    int data = 0;
                    data = GlobalVariable.HandlerActionData[TesterIndex];
                    data &= ~(0x01 << (int)control);
                    GlobalVariable.HandlerActionData[TesterIndex] = (short)data;
                }
            }
        }

        private void cB_Tester1Cell1_Click(object sender, EventArgs e)
        {
            
        }

        private void cB_Tester1Cell1_CheckedChanged(object sender, EventArgs e)
        {
            string name = (sender as CheckBox).Name;
            bool status = (sender as CheckBox).Checked;
            switch (name)
            {
                case "cB_Tester1Cell1":
                    GlobalVariable.HandlerTesterCellData1[TesterIndex] = status ? (short)HandlerPutOutFlag.有产品 : (short)HandlerPutOutFlag.无产品;
                    break;
                case "cB_Tester1Cell2":
                    GlobalVariable.HandlerTesterCellData2[TesterIndex] = status ? (short)HandlerPutOutFlag.有产品 : (short)HandlerPutOutFlag.无产品;
                    break;
                case "cB_Tester1Cell3":
                    GlobalVariable.HandlerTesterCellData3[TesterIndex] = status ? (short)HandlerPutOutFlag.有产品 : (short)HandlerPutOutFlag.无产品;
                    break;
                case "cB_Tester1Cell4":
                    GlobalVariable.HandlerTesterCellData4[TesterIndex] = status ? (short)HandlerPutOutFlag.有产品 : (short)HandlerPutOutFlag.无产品;
                    break;
                case "cB_Tester2Cell1":
                    GlobalVariable.HandlerTesterCellData1[TesterIndex] = status ? (short)HandlerPutOutFlag.有产品 : (short)HandlerPutOutFlag.无产品;
                    break;
                case "cB_Tester2Cell2":
                    GlobalVariable.HandlerTesterCellData2[TesterIndex] = status ? (short)HandlerPutOutFlag.有产品 : (short)HandlerPutOutFlag.无产品;
                    break;
                case "cB_Tester2Cell3":
                    GlobalVariable.HandlerTesterCellData3[TesterIndex] = status ? (short)HandlerPutOutFlag.有产品 : (short)HandlerPutOutFlag.无产品;
                    break;
                case "cB_Tester2Cell4":
                    GlobalVariable.HandlerTesterCellData4[TesterIndex] = status ? (short)HandlerPutOutFlag.有产品 : (short)HandlerPutOutFlag.无产品;
                    break;
            }
        }
    }
}
